import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;

import org.junit.Test;

import com.google.gson.JsonObject;

import utl.mofen.intf.IDBOperator;
import utl.mofen.resultSet.ConvertUtil;
import utl.mofen.script.ScriptObjectFactory;
import utl.mofen.utils.ByteUtil;
import utl.mofen.utils.DateUtil;
import utl.mofen.utils.FileUtil;
import utl.mofen.utils.GsonUtil;
import utl.mofen.utils.JsonSQLTools;
import utl.mofen.utils.StringUtil;

public class UtilTester {
	@Test
	public void ByteUtil_Bytes2String() {
		byte[] yhData = { 35, -46, -69, 35, -70, -29, 35 };
		System.out.println(ByteUtil.bytes2String(yhData));
	}

	@Test
	public void JsonSQLTools_getCondition()
	{
		JsonObject json = new JsonObject();
		json.addProperty("FFormKey", "FB47BBA3-6C21-3485-26BD-5C1A7CBD89D1");
		json.addProperty("FKind", 102001);
		System.out.println(GsonUtil.json2String(json));
		System.out.println(JsonSQLTools.getCondition(json));
		
		
	}

	@Test
	public void ByteUtil_Date() {

		Date date = new Date();

		Calendar c = Calendar.getInstance();
		JsonObject data = new JsonObject();

		data.addProperty("YY", c.get(c.YEAR));
		data.addProperty("MM", c.get(c.MONTH));
		data.addProperty("DD", c.get(c.DATE));
		data.addProperty("hh", c.get(c.HOUR_OF_DAY));
		data.addProperty("mi", c.get(c.MINUTE));
		data.addProperty("ss", c.get(c.SECOND));
		data.addProperty("ms", c.get(c.MILLISECOND));

		System.out.println(GsonUtil.json2String(data));

		System.out.println(date.toLocaleString());
		System.out.println(c.get(Calendar.YEAR));

		System.out.println(date.getMonth());

		System.out.println(c.getTime());
	}

	@Test
	public void ResultSet_ConvertUtil() throws SQLException {
		IDBOperator dbOpera = ScriptObjectFactory.getDBOperator("yh_main");
		String rec = "";
		ResultSet rs = dbOpera.queryData("select * from sys_Users");
		StringBuilder sb = new StringBuilder();
		sb.append("[\n");
		while (rs.next()) {
			rec = ConvertUtil.rs2JsonString(rs, true);
			if (StringUtil.stringIsNotEmpty(rec)) {
				sb.append(rec + ",\n");
			}
		}
		if (sb.length() > 2) {
			sb.deleteCharAt(sb.length() - 2);
		}
		sb.append("]\n");
		System.out.println(sb.toString());

		JsonObject json = new JsonObject();
		json.add("list", GsonUtil.parse(sb.toString()));
		System.out.println(GsonUtil.json2String(json));
	}

	@Test
	public void DateUtil() throws Exception {
		Calendar c = Calendar.getInstance();

		System.out.println(c.get(Calendar.YEAR));
		System.out.println(c.get(Calendar.MONTH));
		System.out.println(c.get(Calendar.DATE));
		System.out.println(c.get(Calendar.HOUR_OF_DAY));
		// data.addProperty("MM", c.get(Calendar.MONTH));
		// data.addProperty("DD", c.get(Calendar.DATE));
		// data.addProperty("hh", c.get(Calendar.HOUR_OF_DAY));
		// data.addProperty("mi", c.get(Calendar.MINUTE));
		// data.addProperty("ss", c.get(Calendar.SECOND));
		// data.addProperty("ms", c.get(Calendar.MILLISECOND));

		// String dateStr = "2012-12-05 18:58:02.654";
		// System.out.println(utl.mofen.utils.DateUtil.stringToDate(dateStr));
		// System.out.println(utl.mofen.utils.DateUtil.timestampToDate(
		// utl.mofen.utils.DateUtil.stringToDateTime(dateStr)));
		// System.out.println(utl.mofen.utils.DateUtil.stringToTimestamp(dateStr));
	}

	@Test
	public void rename() throws Exception {
		if (FileUtil.rename("C:\\1.frf", "1.refrf")) {
			System.out.println("文件修改成功!");
		} else {
			System.out.println("文件修改失败");
		}
	}

	@Test
	public void GsonUtil_jsn2File() throws Exception {
		JsonObject json = new JsonObject();
		json.addProperty("id", "abc");
		json.addProperty("lastTime", DateUtil.dateTime2Str(null));
		GsonUtil.json2File(json, "C:\\abc\\a.jsn");
	}
}
